/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Action;

import ActionForm.MantenimientoMesaActionForm;
import BE.Cliente;
import BE.Mesa;
import BL.ClienteBL;
import BL.MesaBL;
import java.util.Date;
import java.util.ArrayList;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.actions.LookupDispatchAction;

/**
 *
 * @author Xatuuu
 */
public class MantenimientoMesaAction extends LookupDispatchAction {

    /* forward name="success" path="" */
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward registrarMesa(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        MantenimientoMesaActionForm MantenimientoMesaActionForm = (MantenimientoMesaActionForm) form;
        return mapping.findForward("registrarMesa");
    }

    public ActionForward guardarMesa(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        MantenimientoMesaActionForm MantenimientoMesaActionForm = (MantenimientoMesaActionForm) form;
//        DateeTime fecha= new Date();

        Mesa objMesa = new Mesa();
        objMesa.setCvNombre(MantenimientoMesaActionForm.getStrNombreMesa());
        objMesa.setCvDescripcion(MantenimientoMesaActionForm.getStrDescripcionMesa());
        objMesa.setCvDia(MantenimientoMesaActionForm.getStrDiaSemana());
//        objMesa.setDateFecharegistro(fecha.getYear())

        MesaBL objMesaBL = new MesaBL();
        if (objMesaBL.insertarMesa(objMesa)) {

            ArrayList<Cliente> arrClientesAsignados = (ArrayList<Cliente>) MantenimientoMesaActionForm.getArrClientesAsignados();

            ClienteBL objClienteBL = null;
            for (Iterator<Cliente> it = arrClientesAsignados.iterator(); it.hasNext();) {
                Cliente cliente = it.next();
                cliente.setMesa(objMesa);
                objClienteBL = new ClienteBL();
                if (!objClienteBL.modificarCliente(cliente)) {
                    break;
                }
            }
        }

        return mapping.findForward("guardarMesa");
    }

    public ActionForward buscarCliente(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        MantenimientoMesaActionForm MantenimientoMesaActionForm = (MantenimientoMesaActionForm) form;

        ClienteBL objClienteBL = new ClienteBL();
        ArrayList<Cliente> arrClientes = (ArrayList<Cliente>) objClienteBL.seleccionarCliente();
        MantenimientoMesaActionForm.setArrClientesBuscados(arrClientes);

        HttpSession session = request.getSession();
        session.setAttribute("arrClientesBuscados", arrClientes);

        MantenimientoMesaActionForm.setArrClientesOriginales(arrClientes);

        return mapping.findForward("registroMesa");
    }

    public ActionForward agregarTodo(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        MantenimientoMesaActionForm MantenimientoMesaActionForm = (MantenimientoMesaActionForm) form;


        ArrayList<Cliente> arrClientesAsignados = (ArrayList<Cliente>) MantenimientoMesaActionForm.getArrClientesOriginales();

        HttpSession session = request.getSession();

        MantenimientoMesaActionForm.setArrClientesBuscados(new ArrayList());

        session.setAttribute("arrClientesBuscados", new ArrayList());

        session.setAttribute("arrClientesAsignados", arrClientesAsignados);

        MantenimientoMesaActionForm.setArrClientesAsignados(arrClientesAsignados);

        MantenimientoMesaActionForm.setIntClientesAsignados(arrClientesAsignados.size());

        return mapping.findForward("agregaTodosClientes");
    }

    public ActionForward quitarTodo(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        MantenimientoMesaActionForm MantenimientoMesaActionForm = (MantenimientoMesaActionForm) form;


        ArrayList<Cliente> arrClientesBuscados = (ArrayList<Cliente>) MantenimientoMesaActionForm.getArrClientesOriginales();

        HttpSession session = request.getSession();

        MantenimientoMesaActionForm.setArrClientesAsignados(new ArrayList());

        session.setAttribute("arrClientesAsignados", new ArrayList());

        session.setAttribute("arrClientesBuscados", arrClientesBuscados);

        MantenimientoMesaActionForm.setArrClientesBuscados(arrClientesBuscados);

        MantenimientoMesaActionForm.setIntClientesAsignados(0);

        return mapping.findForward("quitarTodosClientes");
    }

    @Override
    protected Map getKeyMethodMap() {
        Map map = new HashMap();
        map.put("button.GuardarMesa", "guardarMesa");
        map.put("button.BuscarCliente", "buscarCliente");
        map.put("button.AgregarTodo", "agregarTodo");
        map.put("button.Agregar", "agregar");
        map.put("button.QuitarTodo", "quitarTodo");
        map.put("button.Quitar", "quitar");
        return map;
    }

    public ActionForward unspecified(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) {

        return mapping.findForward("registrarMesa");

    }
}
